// pages/user_center/modify_password/modify_password.js
Page({
  data:{
    formError:false,
    errorMessage:'未知错误'
  },
  onLoad:function(options){
    // 页面初始化 options为页面跳转所带来的参数
  },
  onReady:function(){
    // 页面渲染完成
  },
  onShow:function(){
    // 页面显示
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  },

  showError:function(msg){
    var that = this;
    this.setData({
      formError: true,
      errorMessage: msg
    });

    setTimeout(function(){
        that.setData({
            formError: false,
            errorMessage: '未知错误'
        });
    }, 2000);
  },

  checkForm:function(e){
    var that = this;
    var appInstance = getApp();

    var object = e.detail.value;

    var oldPassword = object.oldPassword;
    var newPassword = object.newPassword;
    var newPassword2 = object.newPassword2;

    if(!oldPassword){
      that.showError('旧密码不能为空');
    }else if(!newPassword){
      that.showError('新密码不能为空');
    }else if(!newPassword2){
      that.showError('请再次确认新密码');
    }else if(newPassword != newPassword2){
      that.showError('两次密码不一致');
    }else{
      var username = wx.getStorageSync('username');

      wx.request({
        url: 'https://www.lanshuicar.com/Server/FindPassword.ashx',
        data: {
          Meth: '1',
          Tel: username,
          OidPwd: oldPassword,
          NewPwd: newPassword
        },
        method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
        // header: {}, // 设置请求的 header
        success: function(res){
          // success
          console.log('modifyPassword',res.data);
          var code = res.data.code;
          if(code == 0){

            appInstance.globalData.hasLogin = false;
            appInstance.globalData.hasLogout = true;
            wx.clearStorageSync();

            wx.showToast({
              title: '修改成功',
              icon: 'success',
              duration: 2000
            });

            wx.navigateBack({
              delta: 1, // 回退前 delta(默认为1) 页面
              success: function(res){
                // success
              },
              fail: function() {
                // fail
                that.showError('网络异常');
              },
              complete: function() {
                // complete
              }
            })
          }else{
            that.showError(res.data.msg);
          }
        },

        fail: function() {
          // fail
          showError('网络超时')
        },
        complete: function() {
          // complete
        }
      })
    }
  }
})